#!/bin/env bash
<%= ZTK::Template.do_not_edit_notice(:message => "Chef OmniTruck Bootstrap") %>
set -x
set -e

export DEBIAN_FRONTEND="noninteractive"
export CHEF_SOLO_ROOT="/tmp/chef-solo"
export SUDO_USER="<%= @sudo_user %>"
export SUDO_UID="<%= @sudo_uid %>"
export SUDO_GID="<%= @sudo_gid %>"
export HOME="<%= @home_dir %>"
export KNIFE_CONFIG_EXP_FILE="/tmp/knife.exp"
export

mkdir -p ${CHEF_SOLO_ROOT}
cd ${CHEF_SOLO_ROOT}

apt-get -qy update
apt-get -qy install build-essential expect wget curl libgecode-dev ca-certificates

mkdir -pv /etc/chef /var/log/chef ${HOME}/.chef

cat <<EOF | tee /etc/chef/solo.rb
file_cache_path "${CHEF_SOLO_ROOT}/"
cookbook_path %w(${CHEF_SOLO_ROOT}/cookbooks/)
role_path "${CHEF_SOLO_ROOT}/roles/"
EOF

cat <<EOF | tee ${CHEF_SOLO_ROOT}/attributes.json
<%= @chef_solo_attributes.to_json %>
EOF

rm -fv ${CHEF_SOLO_ROOT}/install.sh
wget -v https://www.opscode.com/chef/install.sh -O ${CHEF_SOLO_ROOT}/install.sh
bash ${CHEF_SOLO_ROOT}/install.sh -v <%= @chef[:server][:version] %>
mkdir -pv /var/chef/cache ${CHEF_SOLO_ROOT}/cookbooks/chef-server
wget -qO- https://github.com/opscode-cookbooks/chef-server/archive/master.tar.gz | tar xvzC ${CHEF_SOLO_ROOT}/cookbooks/chef-server --strip-components=1

set +e
chef-solo --config /etc/chef/solo.rb --json-attributes ${CHEF_SOLO_ROOT}/attributes.json --logfile /var/log/chef/chef-solo.log --log_level debug
set -e

cat << EOF | tee /etc/chef-server/chef-server.rb
<%= ZTK::Template.do_not_edit_notice(:message => "OmniTruck Bootstrap") %>
server_name = "<%= @server_name %>"
#
topology                "standalone"
api_fqdn                server_name
nginx['url']          = "https://#{server_name}"
nginx['server_name']  = server_name
lb['fqdn']            = server_name
bookshelf['vip']      = server_name
EOF
chef-server-ctl reconfigure

echo -n "Waiting on <%= File.basename(@chef_validator) %> and <%= File.basename(@chef_webui) %> to appear..."
until [ -f <%= @chef_validator %> ] && [ -f <%= @chef_webui %> ]; do
  echo -n "."
  sleep 1
done
echo "done."

cp -v <%= @chef_validator %> <%= @chef_webui %> ~/.chef
[ -f /etc/chef/validation.pem ] || ln -sv <%= @chef_validator %> /etc/chef/validation.pem
[ -f /etc/chef/admin.pem ] || ln -sv <%= @chef_admin %> /etc/chef/admin.pem

cat << EOF | tee ${KNIFE_CONFIG_EXP_FILE}
#!/usr/bin/expect -f
set timeout 10
spawn knife configure --initial --server-url <%= @chef[:server][:server_url] %> --admin-client-key <%= @chef_admin %> --user <%= @sudo_user %> --repository '' --defaults --yes -VV
expect "Please enter a password for the new user:" { send "<%= @default_password %>\n" }
interact
EOF
chmod -v +x ${KNIFE_CONFIG_EXP_FILE}
[ -f <%= @home_dir %>/.chef/<%= @sudo_user %>.pem ] || ${KNIFE_CONFIG_EXP_FILE}

[ -f <%= @home_dir %>/.chef/<%= @local_user %>.pem ] || knife client create <%= @local_user %> --server-url <%= @chef[:server][:server_url] %> --user <%= @sudo_user %> --key <%= @home_dir %>/.chef/<%= @sudo_user %>.pem --admin --file <%= @home_dir %>/.chef/<%= @local_user %>.pem --editor echo --disable-editing --defaults --yes -VV

chown -Rv ${SUDO_USER}:${SUDO_USER} ${HOME}

touch /.omni-truck-provision
